package dk.modified.reshary;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import dk.modified.reshary.GlobalState.TrackerName;

public class SignupScreen extends Activity implements OnFocusChangeListener {
	private boolean success;
	EditText txt_username;
	EditText txt_realname;
	EditText txt_email;
	EditText txt_password;
	TextView label_txt_username;
	TextView label_txt_realname;
	TextView label_txt_email;
	TextView label_txt_password;

	Button cmd_createuser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup_screen);
		
		
	    Tracker t = ((GlobalState) getApplication()).getTracker(
		        TrackerName.APP_TRACKER);
		    t.setScreenName("Reshary SignupScreen");
		    t.send(new HitBuilders.AppViewBuilder().build());
		
		
		success = true;
		cmd_createuser = (Button) findViewById(R.id.cmd_createuser);
		txt_username = (EditText) findViewById(R.id.txtusername);
		txt_username.setOnFocusChangeListener(this);
		txt_realname = (EditText) findViewById(R.id.txtrealname);
		txt_realname.setOnFocusChangeListener(this);
		txt_email = (EditText) findViewById(R.id.txtemail);
		txt_email.setOnFocusChangeListener(this);
		txt_password = (EditText) findViewById(R.id.txtpassword);
		txt_password.setOnFocusChangeListener(this);
		label_txt_username = (TextView) findViewById(R.id.label_txtusername);
		label_txt_realname = (TextView) findViewById(R.id.label_txtrealname);
		label_txt_email = (TextView) findViewById(R.id.label_txtemail);
		label_txt_password = (TextView) findViewById(R.id.label_txtpassword);
	}

	

	
	public void createUserClick(View v) {
		success = true;
		if (!SignupController.ValidateUsername(txt_username.getText()
				.toString().trim())) {
			label_txt_username.setVisibility(View.VISIBLE);
			success = false;
		}

		if (!SignupController.ValidatePassword(txt_password.getText()
				.toString().trim())) {
			label_txt_password.setVisibility(View.VISIBLE);
			success = false;
		}
		if (!SignupController.ValidateRealname(txt_realname.getText()
				.toString().trim())) {
			label_txt_realname.setVisibility(View.VISIBLE);
			success = false;
		}
		if (!SignupController.ValidateEmail(txt_email.getText().toString()
				.trim())) {
			label_txt_email.setVisibility(View.VISIBLE);
			success = false;
		}

		if (success) {
			SharedPreferences sharedpreferences = this.getSharedPreferences(
					"dk.modified.reshary", Context.MODE_PRIVATE);
			Editor editor = sharedpreferences.edit();
			editor.putString("email", txt_email.getText().toString().trim());
			editor.commit();
			cmd_createuser.setEnabled(false);
			PostSignup();
		}
	}

	
	
	
	
	private void PostSignup() {
		RequestParams params = new RequestParams();
		String username = txt_username.getText().toString().trim();
		String realname = txt_realname.getText().toString().trim();
		String email = txt_email.getText().toString().trim();
		String password = txt_password.getText().toString().trim();

		params.put("realname", realname);
		params.put("username", username);
		params.put("email", email);
		params.put("pass", password);

		ResharyRestClient.post("register/", params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onFailure(int statusCode, Throwable error,
							String content) {
						cmd_createuser.setEnabled(true);
						Toast.makeText(
								getApplicationContext(),
								getResources()
										.getString(R.string.network_error),
								Toast.LENGTH_LONG).show();

					}

					@Override
					public void onSuccess(String content) {
						cmd_createuser.setEnabled(true);
						Map<String, String> postResponse = Util
								.newformatResponse(content);
						if (postResponse.containsKey("status")) {
							if (postResponse.get("status").equals("success")) {
								Util.sharedPrefsPutLogin(
										getApplicationContext(), postResponse);
								Util.intentLogin(getApplicationContext());
							} else {
								if (postResponse.containsKey("pass")) {
									label_txt_password
											.setVisibility(View.VISIBLE);
								}
								if (postResponse.containsKey("email")) {
									label_txt_email.setVisibility(View.VISIBLE);
								} 
								if (postResponse.containsKey("username")) {
									label_txt_username
											.setVisibility(View.VISIBLE);
								}
//								} else {
//									Toast.makeText(getApplicationContext(),
//											postResponse.toString(),
//											Toast.LENGTH_LONG).show();
//								}

							}
						}
					}

				}, this);
	}

	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.right_out, R.anim.left_in);
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.txtusername) {
			if (hasFocus) {
				label_txt_username.setVisibility(View.INVISIBLE);
			}
		}

		if (v.getId() == R.id.txtrealname) {
			if (hasFocus) {
				label_txt_realname.setVisibility(View.INVISIBLE);
			}

		}

		if (v.getId() == R.id.txtemail) {
			if (hasFocus) {
				label_txt_email.setVisibility(View.INVISIBLE);
			}
		}

		if (v.getId() == R.id.txtpassword) {
			if (hasFocus) {
				label_txt_password.setVisibility(View.INVISIBLE);
			}
		}

	}

}
